"""
异步的方式下载视觉中国茶叶相关图片
https://www.vcg.com/creative-image/chaye/
"""
import hashlib
import json
import os

import arequests
import re

from lxml import etree


def md5(script_str):
    m = hashlib.md5()
    m.update(script_str.encode("utf8"))
    return m.hexdigest()


headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61"
                  " Safari/537.36"
}


def parse_get_img_src(html):
    """
    解析到图片url
    :param html:列表页整体的html
    :return:图片url列表
    """
    ex = 'window.__PRELOADED_STATE__ = (.*?)</script><script'
    data = re.findall(ex, html)[0]
    json_obj = json.loads(data)
    list_data = json_obj['searchImage']['data']['list']
    flash_list = []
    for i in list_data:
        flash_data = {
            'unique_key': md5(str(i['id'])),
            'src': 'http:' + i['src'],
            'createdTime': i['createdTime'],
        }
        flash_list.append(flash_data)
    return flash_list


def get_dir():
    """ 不存在则创建目录"""
    # 项目跟路径
    root_path = os.path.abspath(os.path.dirname(__file__)).split('tea_info_spider')[0]
    img_dir = 'tea_images'
    directory = root_path + img_dir
    if not os.path.exists(directory):
        os.mkdir(directory)
    return directory


def download_img(img_data):
    # 下载图片
    for i in img_data:
        img_url = i['src']
        print(img_url, '正在下载。。。')
        resp = arequests.get(img_url)
        b = resp.content
        file_name = img_url.split('/')[-1]
        file_full_path = get_dir() + '\'' + file_name
        with open(file_full_path, 'wb') as fp:
            fp.write(b)
            print(file_full_path, '下载成功')


def save_data():
    # 保存到数据库
    pass


def upload_img_chr():
    # 上传图床
    pass


def parse_next_page(html):
    # 解析下一页
    selector = etree.HTML(html)
    next_page_path = "//div[@class='center_page']/a[@class='paginationNowPage']/following-sibling::a[1]/@href"
    # 下一页的url
    next_page_url = selector.xpath(next_page_path)[0]
    return next_page_url

url = 'https://www.vcg.com/creative-image/chaye/'
response = arequests.get(url, headers=headers)
if response.status_code == 200:
    page_html = response.text
    a = parse_next_page(page_html)
    print(a)
    # img_src_data = parse_get_img_src(page_html)
    # download_img(img_src_data)

